package com.mmy.controller.visa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.visa.TVisaBase;
import com.mmy.domain.visa.TVisaOrder;
import com.mmy.domain.visa.TVisaStuff;
import com.mmy.domain.visa.VVisaBase;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.visa.TVisaBaseServiceImpl;
import com.mmy.service.impl.visa.TVisaOrderServiceImpl;
import com.mmy.service.impl.visa.TVisaStuffServiceImpl;
import com.mmy.service.impl.visa.VVisaBaseServiceImpl;

/**
 * 类的介绍：签证基础信息
 *
 * @author hanxl
 * @date 2020-07-23
 * @version 1.0
 */
@Controller
public class TVisaBaseController {
	@Autowired
	TVisaBaseServiceImpl tVisaBaseServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	TVisaOrderServiceImpl tVisaOrderServiceImpl;
	@Autowired
	TVisaStuffServiceImpl tVisaStuffServiceImpl;
	@Autowired
	VVisaBaseServiceImpl vVisaBaseServiceImpl;
	@Autowired
	Common common;

	/* 显示签证基本信息首页 */
	@RequestMapping(value = "/visaindex")
	public String FindAll(Model mConstZone, Model mVisaBase) {
		/* 查询所有区域 洲与国家 */
		List<TConstZone> db_TConstZoneContinent = constZoneServiceImpl.findAllContinents();
		mConstZone.addAttribute("newTConstZoneContinent", db_TConstZoneContinent);
		List<TConstZone> db_TConstZoneCountriess = constZoneServiceImpl.findAllCountriess();
		mConstZone.addAttribute("newTConstZoneCountriess", db_TConstZoneCountriess);
		/* 获得所有签证 */
		List<VVisaBase> db_VVisaBase = vVisaBaseServiceImpl.findAll();
		mVisaBase.addAttribute("newVVisaBase", db_VVisaBase);

		return "webbackground/visamanagement/visaindex";
	}

	/**
	 * 函数功能说明： 查看签证基本信息详情
	 *
	 * @param 参数说明 tVisaBase
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.29
	 */

	@RequestMapping(value = "/visainfo")
	public String ToVisaInfo(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("vVisaBase", vVisaBase);
		List<TVisaStuff> lstVisaStuff = tVisaStuffServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("newTVisaStuff", lstVisaStuff);
		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		return "webbackground/visamanagement/visainfo";
	}

	@RequestMapping(value = "/ordervisainfo")
	public String ToOrderVisaInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);

		String strOrderID = tVisaOrder.getOrderID();
		int iVisaID = Integer.parseInt(strOrderID.substring(0, 9));

		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);

		model.addAttribute("vVisaBase", vVisaBase);

		return "webbackground/visamanagement/ordervisainfo";
	}

//收款打印页面
	@RequestMapping(value = "/printvisa")
	public String ToPrintVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);

		return "webbackground/visamanagement/printvisa";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelvisa")
	public String ToPrintCancelVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);

		return "webbackground/visamanagement/printcancelvisa";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashiervisa")
	public String ToCashierVisa(Model model) {
		try {

			List<VVisaBase> lstVisaBase = vVisaBaseServiceImpl.findAll();
			model.addAttribute("tVisaBase", lstVisaBase);

			return "OA/cashier/cashiervisa";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashiervisaorder")
	public String ToCashierVisaOrder(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));

		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		model.addAttribute("iVisaID", iVisaID);

		return "OA/cashier/cashiervisaorder";
	}

	@RequestMapping(value = "/entercheckcashiervisa")
	public String EnterCheckCashierVisa(HttpServletRequest request, HttpSession session, String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<TVisaOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(i);

			if (tVisaOrder.getCashierState() == 0) {
				tVisaOrder.setCashierState((byte) 1);
			} else if (tVisaOrder.getCashierState() == 1) {
				tVisaOrder.setCashierState((byte) 2);
			} else {
				tVisaOrder.setCashierState((byte) 1);
			}
			tVisaOrder.setCashierDate(new Date());
			tVisaOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tVisaOrder);
		}

		tVisaOrderServiceImpl.saveAll(lstCashier);

		return "OA/cashier/cashiervisaorder";

	}

//查询
	@RequestMapping(value = "/searchcashiervisa")
	public String FindCashierVisaOrder(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
			model.addAttribute("orderlist", lstVisaOrder);
			model.addAttribute("iVisaID", iVisaID);
			return "OA/cashier/cashiervisaorder";
		} else {
			List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByCashierAndVisaID(strCashierState, iVisaID);
			model.addAttribute("orderlist", lstVisaOrder);
			model.addAttribute("iVisaID", iVisaID);
			return "OA/cashier/cashiervisaorder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accountvisa")
	public String ToAccountVisa(HttpServletRequest request, Model model) {

		List<VVisaBase> lstVisaBase = vVisaBaseServiceImpl.findAll();
		model.addAttribute("tVisaBase", lstVisaBase);

		return "OA/account/accountvisa";
	}

	@RequestMapping(value = "/accountvisaorder")
	public String ToAccountVisaOrder(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));

		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		model.addAttribute("iVisaID", iVisaID);

		return "OA/account/accountvisaorder";
	}

	@RequestMapping(value = "/entercheckaccountvisa")
	public String EnterCheckAccountVisa(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<TVisaOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(i);

			if (tVisaOrder.getAccountState() == 0) {
				tVisaOrder.setAccountState((byte) 1);
			} else if (tVisaOrder.getAccountState() == 1) {
				tVisaOrder.setAccountState((byte) 2);
			} else {
				tVisaOrder.setAccountState((byte) 1);
			}
			tVisaOrder.setAccountDate(new Date());
			tVisaOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tVisaOrder);
		}

		tVisaOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accountvisaorder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittancevisa")
	public String EnterCheckRemittanceVisa(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<TVisaOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(i);

			if (tVisaOrder.getRemittanceState() == 0) {
				tVisaOrder.setRemittanceState((byte) 1);
			} else {
				tVisaOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tVisaOrder);
		}

		tVisaOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accountscenicorder";

	}

//查询
	@RequestMapping(value = "/searchaccountvisa")
	public String FindAccountVisa(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
			model.addAttribute("orderlist", lstVisaOrder);
			model.addAttribute("iVisaID", iVisaID);

			return "OA/account/accountvisaorder";
		} else {
			List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByAccountAndVisaID(strAccountState, iVisaID);
			model.addAttribute("orderlist", lstVisaOrder);
			model.addAttribute("iVisaID", iVisaID);

			return "OA/account/accountvisaorder";
		}

	}

	/* 查询酒店信息 */
	@RequestMapping(value = "/searchvisabase")
	public String FindVisaBase(HttpServletRequest request, Model model) {

		String strVisaName = request.getParameter("VisaName");
		String strZoneID = request.getParameter("ZoneID");
		String strType = request.getParameter("Type");
		List<VVisaBase> lstVisaBase = new ArrayList<VVisaBase>();
		if (strVisaName == "" && strZoneID == "" && strType == "" && strType == "") {
			lstVisaBase = vVisaBaseServiceImpl.findAll();

		} else {
			lstVisaBase = vVisaBaseServiceImpl.findBySearch(strVisaName, strZoneID, strType);

		}
		model.addAttribute("newVVisaBase", lstVisaBase);

		/* 查询所有区域 洲与国家 */
		List<TConstZone> db_TConstZoneContinent = constZoneServiceImpl.findAllContinents();
		model.addAttribute("newTConstZoneContinent", db_TConstZoneContinent);
		List<TConstZone> db_TConstZoneCountriess = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("newTConstZoneCountriess", db_TConstZoneCountriess);
		request.setAttribute("strVisaName", strVisaName);
		return "webbackground/visamanagement/visaindex";

	}

	/* 提交修改的签证信息 */
	@RequestMapping(value = "/saveupdatevisabase")
	public String UpdateVisaBase(TVisaBase tVisaBase, HttpSession session) throws Exception {
		int iVisaID = tVisaBase.getVisaID();

		/* 定义一个新的实体类对象 */
		TVisaBase dtVisaBase = tVisaBaseServiceImpl.findByiVisaID(iVisaID).get(0);

		dtVisaBase.setVisaName(tVisaBase.getVisaName());
		dtVisaBase.setZoneID(tVisaBase.getZoneID());
		dtVisaBase.setDay(tVisaBase.getDay());
		dtVisaBase.setFloor(tVisaBase.getFloor());
		dtVisaBase.setPeer(tVisaBase.getPeer());
		dtVisaBase.setPrice(tVisaBase.getPrice());
		dtVisaBase.setMemo(tVisaBase.getMemo());
		dtVisaBase.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtVisaBase.setReviser(tSysUser.getUserName());

		tVisaBaseServiceImpl.save(dtVisaBase);

		return "redirect:visaindex";

	}

	/**
	 * 函数功能说明： 删除签证
	 *
	 * @param 参数说明 tVisaBase
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.29
	 */
	@PostMapping("/deleteVisaBaseChecked")
	@ResponseBody
	/* @RequestMapping(value="/deleteChecked") */
	public String deleteChecked(String checkList) {

		System.out.println("==>" + checkList);

		String[] striVisaIDs = checkList.split(",");
		List<Integer> iVisaIDs = new ArrayList<>();

		for (String striVisaID : striVisaIDs) {
			iVisaIDs.add(Integer.parseInt(striVisaID));
		}

		tVisaBaseServiceImpl.deleteChecked(iVisaIDs);

		return "redirect:visaindex";
	}
}
